package com.huitone.gddw.controller.monitoring;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.dao.entity.UserGate;
import com.huitone.gddw.log.Log;
import com.huitone.gddw.log.LogResult;
import com.huitone.gddw.log.LogType;
import com.huitone.gddw.model.support.Message;
import com.huitone.gddw.service.monitoring.IUserGateSerivce;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 用户通道表
 * @author wuluodan
 *
 */
@Controller
@RequestMapping(value="userGate")
public class UserGateController {

	private final static Logger LOGGER = Logger.getLogger(UserGateController.class);
	
	@Autowired
	private IUserGateSerivce userGateSerivce;
	
	/**
	 * 用户需要显示的通道信息
	 * @param request
	 * @return List<UserGate>
	 */
	@ResponseBody
	@RequestMapping(value="find")
	@Log(logType=LogType.SELECT, content="查询用户需要显示的通道信息")
	public List<UserGate> find(HttpServletRequest request) {
		try {
			Map<String, Object> params = new HashMap<>();
			params.put("userId", SysLoginUtil.getLoginUserId());
			return userGateSerivce.find(params);
		} catch (Exception e) {
			LOGGER.error("查询用户需要显示的通道信息过程中出现异常：" + e.getMessage(), e);
			request.setAttribute("logResult", LogResult.EXCEPTION);
			return null;
		}
	}
	
	/**
	 * 保存
	 * @param request
	 * @return List<UserGate>
	 */
	@ResponseBody
	@RequestMapping(value="update")
	@Log(logType=LogType.UPDATE, content="保存用户通道信息")
	public Message update(HttpServletRequest request, String userId, String gateLinkIds) {
		List<String> listGateLinkId = StringUtils.isBlank(gateLinkIds) ? new ArrayList<String>() : Arrays.asList(gateLinkIds.split(","));
		List<String> listAllGateLinkId = new ArrayList<String>();
		List<String> listSave = new ArrayList<String>();
		List<String> listDel = new ArrayList<String>();
		try {
			userId = SysLoginUtil.getLoginUserId();
			Map<String, Object> params = new HashMap<>();
			params.put("userId", userId);
			List<UserGate> listUserGate = userGateSerivce.find(params);
			for (UserGate userGate : listUserGate) {
				listAllGateLinkId.add(userGate.getGateLinkId());
			}
			for (String gateLinkId : listGateLinkId) {
				if (!listAllGateLinkId.contains(gateLinkId)) {
					listSave.add(gateLinkId);
				}
			}
			for (String gateLinkId : listAllGateLinkId) {
				if (!listGateLinkId.contains(gateLinkId)) {
					listDel.add(gateLinkId);
				}
			}
			userGateSerivce.updateGateLinkId(userId, listSave, listDel);
			return new Message(true, "保存成功！");
		} catch (Exception e) {
			LOGGER.error("保存用户通道信息过程中出现异常：" + e.getMessage(), e);
			request.setAttribute("logResult", LogResult.EXCEPTION);
			return new Message(false, "保存失败！");
		}
	}
	
}
